package com.example.demo.mapper;

import com.example.demo.entity.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface CommentMapper {
    // 添加评论
    @Insert("INSERT INTO comment (post_id, user_id, parent_comment_id, content) " +
            "VALUES (#{postId}, #{userId}, #{parentCommentId}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 自动生成主键 ID
    void insertComment(Comment comment);
    // 获取帖子的评论列表（只返回一级评论）
    @Select("SELECT id, user_id AS userId, post_id AS postId, content, created_at AS createdAt " +
            "FROM comment WHERE post_id = #{postId} AND parent_comment_id IS NULL ORDER BY created_at DESC")
    List<Comment> getCommentsByPostId(@Param("postId") Long postId);

    // 获取二级评论列表
    @Select("SELECT id, user_id AS userId, post_id AS postId, content, created_at AS createdAt " +
            "FROM comment WHERE parent_comment_id = #{parentCommentId} ORDER BY created_at DESC")
    List<Comment> getRepliesByParentCommentId(@Param("parentCommentId") Long parentCommentId);

    // 获取帖子的评论总数
    @Select("SELECT COUNT(*) FROM comment WHERE post_id = #{postId}")
    int getCommentCountByPostId(@Param("postId") Long postId);

    // 获取帖子的二级评论总数
    @Select("SELECT COUNT(*) FROM comment WHERE post_id = #{postId} AND parent_comment_id IS NOT NULL")
    int getReplyCountByPostId(@Param("postId") Long postId);
}